package com.weiyin.examination.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weiyin.examination.R;
import com.weiyin.examination.application.Constant;
import com.weiyin.examination.http.RetrofitClient;
import com.weiyin.examination.module.UserModule;
import com.weiyin.examination.util.LayoutHelper;

import org.apache.commons.lang3.StringUtils;

/**
 * 查看个人信息界面
 *
 * @author njf
 */
public class PersonActivity extends BaseActivity implements View.OnClickListener {

    private Button personSubmit;
    private UserModule userModule;
    private ImageView personHeadImageView;
    private TextView personNumber, personName, personNamePy, personSex, personAge, personCity, personFamilyName, personSubject, personLevel, personCentreNo;
    private EditText personTestCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person);
        LayoutHelper layoutHelper = new LayoutHelper(this);
        layoutHelper.scaleView(findViewById(R.id.personActivityLayout));
        userModule = getIntent().getParcelableExtra("UserModule");
        initView();
        initData();
    }

    private void initView() {
        personSubmit = (Button) findViewById(R.id.personSubmit);

        personNumber = (TextView) findViewById(R.id.personNumber);
        personName = (TextView) findViewById(R.id.personName);
        personNamePy = (TextView) findViewById(R.id.personNamePy);
        personSex = (TextView) findViewById(R.id.personSex);
        personAge = (TextView) findViewById(R.id.personAge);
        personCity = (TextView) findViewById(R.id.personCity);
        personFamilyName = (TextView) findViewById(R.id.personFamilyName);
        personSubject = (TextView) findViewById(R.id.personSubject);
        personLevel = (TextView) findViewById(R.id.personLevel);
        personCentreNo = (TextView) findViewById(R.id.personCentreNo);
        personHeadImageView = (ImageView) findViewById(R.id.personHeadImageView);

        personTestCode = (EditText) findViewById(R.id.personTestCode);

        personSubmit.setOnClickListener(this);
    }

    private void initData() {
        personName.setText(userModule.getName());
        personNumber.setText(userModule.getNumber());
        personNamePy.setText(userModule.getNamePinyin());
        personSex.setText(userModule.getSex());
        personAge.setText(userModule.getBirth());
        personCity.setText(userModule.getCountry());
        personFamilyName.setText(userModule.getNation());
        personSubject.setText(userModule.getSubject());
        personLevel.setText(userModule.getLevel());
        personCentreNo.setText(userModule.getCentreNo());

        Glide.with(this).
                load(RetrofitClient.BASE_URL + userModule.getPicUrl()).
                into(personHeadImageView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.personSubmit:
                String testCode = personTestCode.getText().toString();
                if (StringUtils.isEmpty(testCode)) {
                    showToast("考试码不能为空");
                }
                if (userModule.getTestCode().equals(testCode)) {
                    Intent intent = new Intent();
                    intent.putExtra("UserModule", userModule);
                    intent.setClass(PersonActivity.this, AudioTestActivity.class);
                    startActivity(intent);
                } else {
                    showToast("考试码错误");
                }
                break;
            default:

                break;
        }
    }
}
